package com.github.pig.admin.controller.assess;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.model.zhgl.propertyassess.PropertyAssess;
import com.github.pig.admin.service.user.SysUserService;
import com.github.pig.admin.service.zhgl.propertyassess.IPropertyAssessService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物业人员评价表 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-06-11
 */
@RestController
@RequestMapping("/propertyAssess")
public class PropertyAssessController extends BaseController {
    @Autowired
    private IPropertyAssessService propertyAssessService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return PropertyAssess
     */
    @GetMapping("/{id}")
    public R<PropertyAssess> get(@PathVariable Integer id) {
        return new R<>(propertyAssessService.selectById(id));
    }


    /**
     * 获取所有物业人员信息
     *
     * @return PropertyAssess
     */
//    @RequestMapping(value = "/page",method = RequestMethod.GET)
//    @ResponseBody
//    public Page selectTotal(@RequestParam Map<String, Object> params,SelectTotal selectTotal ) {
//        Map<String, Object> condition=new HashMap<String, Object>();
//        Page<SysUser> page = new Page<SysUser>(Integer.parseInt(params.getOrDefault("page", 1).toString())
//                , Integer.parseInt(params.getOrDefault("limit", 10).toString()));
//        // 获取到当前登录用户
//        Claims claims = getCurrentUserInfo();
//        String currentUserName =  (String) claims.get("user_name");
//        UserVO userVO = sysUserService.findUserByUsername(currentUserName);
//        // 获取到当前登录用户id
//        Integer userId = userVO.getUserId();
//        selectTotal.setUserId(userId);
//        return propertyAssessService.getPropertyUsers(page,selectTotal);
//    }

    /**
     * 点赞/评语
     * @param  propertyAssess  实体
     * @return success/false
     */
    @RequestMapping("/assess")
    public AjaxJson propertyAssess(@RequestBody PropertyAssess propertyAssess) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        String msg = "点赞成功!";
        //判断是否是点赞
        if(CommonConstant.ASSES_DZ.equals(propertyAssess.getBz())){
            //判断当前业主当天是否已经点赞
            EntityWrapper entityWrapper = new EntityWrapper();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            entityWrapper.eq("assess_user",propertyAssess.getAssessUser());
            entityWrapper.eq("property_user_id",propertyAssess.getPropertyUserId());
            entityWrapper.eq("DATE_FORMAT(assess_date, '%Y-%m-%d')",simpleDateFormat.format(new Date()));
            entityWrapper.eq("del_flag",CommonConstant.STATUS_NORMAL);
            List<PropertyAssess> result = propertyAssessService.selectList(entityWrapper);
            if(result.size()>0){
                json.setSuccess(false);
                json.setMsg("您今天已点赞!");
                return json;
            }

        }else{
            msg = "评语成功!";
        }
        propertyAssess.setAssessDate(new Date());
        propertyAssess.setDelFlag(CommonConstant.STATUS_NORMAL);
        propertyAssessService.insert(propertyAssess);
        json.setMsg(msg);
        return json;
    }


}
